const http = require("http");
const fs = require("fs");
const url = require("url");

module.exports = {
    server:function(ops){
        http.createServer((req,res)=>{
            res.writeHead(200,{"Content-Type":"text/html;charset=utf-8"});

            const p = url.parse(req.url).pathname;
            if(p.includes("/api")){
                res.write("你好");
                res.end();
            }else{
                fs.readFile("./" + ops.root + p, (err, data)=>{
                    if(err){
                        res.write("404")
                    }else{
                        res.write(data)
                    }
                    res.end();
                })
            }
        }).listen(ops.port);
    }
}